/**
 * Copyright (2024, ) Institute of Software, Chinese Academy of Sciences
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.ac.iscas.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;

/**
 * 
 * It is used for creating various HttpRequest
 * 
 * @author wuheng@iscas.ac.cn
 * @since 1.0.0
 **/
public class HtmlUtil {
	
	private HtmlUtil() {
		super();
	}

	public static ArrayNode getZkhProds(String url) throws Exception {
//		String url = "https://www.zkh.com/list/c-10292144.html"; // 替换为目标网页 URL

		// 1. 使用 Jsoup 获取网页内容
        Document document = Jsoup.connect(url).get();
        // 2. 提取包含 window.__INITIAL_DATA__ 的 <script> 标签
        Element scriptElement = document.selectFirst("script:containsData(window.__INITIAL_DATA__)");
        if (scriptElement == null) {
            throw new Exception("未找到包含 window.__INITIAL_DATA__ 的 <script> 标签");
        }

        // 3.转化成JSON
        String scriptContent = scriptElement.html()
        		.replaceAll("\\bundefined\\b", "null");
        Pattern pattern = Pattern.compile(
        		"window\\.__INITIAL_DATA__\\s*=\\s*(\\{.*?\\});", 
        		Pattern.DOTALL);
        
        Matcher matcher = pattern.matcher(scriptContent);
        
        if (matcher.find()) {
            String jsonData = matcher.group(1);

            // 4. 使用 Jackson 解析 JSON 数据
            ObjectMapper objectMapper = new ObjectMapper();
            JsonNode rootNode = objectMapper.readTree(jsonData);

            // 5.返回结果
            return (ArrayNode) rootNode.path("catalog")
            		.get("listInfo").get("page").get("content");
        } 
        
        return new ObjectMapper().createArrayNode();
	}
}
